package com.foreknow.eldclient.dao;

import com.foreknow.eldclient.po.Orders;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface OrdersMapper {
    /**
     * 添加一条订单
     * @param orders
     * @return
     * @throws Exception
     */
    @Options(useGeneratedKeys = true,keyProperty = "orderId",keyColumn ="orderId" )
    @Insert(" insert into orders(userId,businessId,orderDate,orderTotal,daId,orderState,deliveryState,remarks)\n" +
            "         values(#{userId},#{businessId},#{orderDate},#{orderTotal},#{daId},0,0,#{remarks})")
    public void saveOrders(Orders orders) throws Exception;

    /**
     * 根据订单ID查询出所有订单信息以及相关的商家信息
     * @param orderId
     * @return
     * @throws Exception
     */
    public Orders getOrdersById(Integer orderId) throws Exception;

    /**
     * 根据用户ID查询出该用户的订单信息以及订单相关商家的信息
     * @param userId
     * @return
     * @throws Exception
     */
    public List<Orders> listOrdersByUserId(String userId) throws Exception;

    /**
     * 更新订单状态
     * @param orderId
     * @return
     * @throws Exception
     */
    @Update("update orders set orderState=1 where orderId=#{orderId}")
    int updateOrderState(Integer orderId) throws  Exception;


    @Select("select count(*) from orders where businessId=#{businessId} and orderState=1")
    int getTotalSales(int businessId) throws Exception;

    @Update("update orders set deliveryState=1 where orderId=#{orderId}")
    int updateDeliState(int orderId);

    /**
     * 查询商家的所支付成功的订单
     */

    List<Orders> listBusinessOrders(int businessId);
}
